package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

	@Reference
	CheckItemService checkItemService;

	@PreAuthorize("hasAuthority('CHECKITEM_ADD')")
	@RequestMapping("/add")
	public Result add(@RequestBody CheckItem checkItem){

		log.debug("CheckItemController==add"+checkItem);

		checkItemService.add(checkItem);

		log.debug("检查项添加成功");

		return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);

	}


	@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
	@RequestMapping("/findPage")
	public Result findPage(@RequestBody QueryPageBean  queryPageBean){

		log.debug("queryPageBean==findPage"+queryPageBean);

		PageResult pageResult = checkItemService.findPage(queryPageBean);

		return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);

	}


	@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
	@RequestMapping("/findById")
	public Result findById(Integer id){

		log.debug("findById==findById"+id);

		CheckItem checkItem = checkItemService.findById(id);

		return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);

	}
	@PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
	@RequestMapping("/edit")
	public Result edit(@RequestBody CheckItem checkItem){
		log.debug("CheckItemController:edit: " + checkItem);
		checkItemService.edit(checkItem);
		log.debug("检查项修改成功!!");
		return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
	}

	@PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
	@RequestMapping("/delById")
	public Result delById(Integer id){
		log.debug("CheckItemController:delete: " + id);
		checkItemService.delById(id);
		log.debug("检查项删除成功!!");
		return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
	}

	@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
	@RequestMapping("/findAll")
	public Result findAll(){
		List<CheckItem> checkItemList =  checkItemService.findAll();
		return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemList);
	}



}
